priority -50

extends c

# We want to overwrite everything in parent ft.
priority -49
###########################################################################
#			    Global functions 						  #
###########################################################################

global !p

def write_docstring_args(arglist, snip):
	args = str(arglist).split(',')

	if len(args) > 1:
		c = 0
		for arg in args:
			if c == 0:
				snip.rv += arg
				c = 1
			else:
				snip += '*       : %s' % arg.strip()
	else:
		snip.rv = args[0]


endglobal

###########################################################################
#                            TextMate Snippets                            #
###########################################################################
snippet beginend "$1.begin(), $1.end() (beginend)"
${1:v}${1/^.*?(-)?(>)?$/(?2::(?1:>:.))/}begin(), $1${1/^.*?(-)?(>)?$/(?2::(?1:>:.))/}end()
endsnippet

snippet cl "class .. (class)"
class ${1:`!p snip.rv = snip.basename or "name"`}{
private:
	${0:}

public:
	${1/(\w+).*/$1/}();
	${1/(\w+).*/$1/}(const ${1/(\w+).*/$1/}&);
	${1/(\w+).*/$1/} &operator=(const ${1/(\w+).*/$1/}&);
	~${1/(\w+).*/$1/}();
};

${1/(\w+).*/$1/}::${1/(\w+).*/$1/}(){
	
}

${1/(\w+).*/$1/}::${1/(\w+).*/$1/}(const ${1/(\w+).*/$1/}&){
	
}

${1/(\w+).*/$1/} &${1/(\w+).*/$1/}::operator=(const ${1/(\w+).*/$1/}&){
	
	return *this;
}

${1/(\w+).*/$1/}::~${1/(\w+).*/$1/}(){
	
}
endsnippet

snippet ns "namespace .. (namespace)"
namespace${1/.+/ /m}${1:`!p snip.rv = snip.basename or "name"`}
{
	${VISUAL}$0
}${1/.+/ \/* /m}$1${1/.+/ *\/ /m}
endsnippet

snippet readfile "read file (readF)"
std::vector<char> v;
if (FILE *fp = fopen(${1:"filename"}, "r"))
{
	char buf[1024];
	while(size_t len = fread(buf, 1, sizeof(buf), fp))
		v.insert(v.end(), buf, buf + len);
	fclose(fp);
}
endsnippet

snippet map "std::map (map)"
std::map<${1:key}, ${2:value}> map$0;
endsnippet

snippet vector "std::vector (v)"
std::vector<${1:char}> v$0;
endsnippet

snippet tp "template <typename ..> (template)"
template <typename ${1:_InputIter}>
endsnippet

snippet cla "An entire .h generator" b
#ifndef ${2:`!v substitute(vim_snippets#Filename('$1_H','ClassName'),'.*','\U&\E','')`}
#define $2

class ${1:`!v substitute(substitute(vim_snippets#Filename('$1','ClassName'),'^.','\u&',''), '_\(\w\)', '\u\1', 'g')`}
{
private:
	$3

public:
	$1();
	virtual ~$1();
};

#endif /* $2 */
endsnippet


snippet fnc "Basic c++ doxygen function template" b
/**
* @brief: ${4:brief}
*
* @param: `!p write_docstring_args(t[3],snip)`
*
* @return: `!p snip.rv = t[1]`
*/
${1:ReturnType} ${2:FunctionName}(${3:param}){
	${0:FunctionBody}
}
endsnippet
# vim:ft=snippets:

# 我的自定义
snippet main "main function generator"
int main(int argc, char *argv[]){
	${1}
	
	return 0;
}
endsnippet

snippet head "inlcude header"
#include <iostream>
${1}
using namespace std;
endsnippet

snippet for "for loop"
for(int i=0; i<${1:n}; i++){
	${0}
}
endsnippet


snippet fun "Basic c++ doxygen function template" b
${1:ReturnType} ${2:FunctionName}(${3:param}){
	${0:FunctionBody}
}
endsnippet

snippet co "cout"
cout<<${1}<<endl;
endsnippet

#UltiSnips自定义snippet的语法为:snippet trigger_word ["description" [options] ]
#以 snippet fnc 为例， trigger_word为fnc，后面紧跟的是描述，只是对该 snippet 的说明, options为b，b意味着只有trigger_word在行首的时候，才执行对应操作; 其中的变量${n}，代表是第几个可以用tab跳转的可输入块，默认开始时光标停留在${1}上，最后一个可跳转位置是${0}。
